<script setup>
import { ref,onMounted } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { useRouter } from "vue-router";
import { getproductsList } from "@/apis/product";
const router = useRouter();
const productList = ref([])
const pageInfo = ref({
  current: 1,
  size: 8,
  total: 0
})
const pushToPath = (path) => {
  router.push(path);
};

const getProducts = async (c=1,t=5) => {
  const res = await getproductsList(c,t);
  console.log(res);
  productList.value = res.records;
  pageInfo.value.total = parseInt(res.total, 10);
  pageInfo.value.size = res.size;
  pageInfo.value.current = res.current;
}

const handlePageChange = (current) => {
  pageInfo.value.current = current;
  getProducts(current);
}


onMounted(() => {
  getProducts();
})
</script>

<template>
  <el-row>
    <el-button :icon="Plus" @click="pushToPath('/admin/product/add')">新增</el-button>
  </el-row>
  <el-row>
    <el-table :data="productList" style="width: 100%">
      <el-table-column prop="id" label="ID"  />
      <el-table-column prop="name" label="名称"  /> 
      <el-table-column prop="subTitle" label="描述"  /> 
      <el-table-column prop="mainImage" label="封面"  >
        <template #default="scope">
          <img :src="'/api/images/'+scope.row.mainImage" alt="" style="width: 100px; height: 100px;">
        </template>
      </el-table-column> 
      <el-table-column prop="categoryId" label="分类ID" /> 
      <el-table-column prop="price" label="价格" /> 
      <el-table-column prop="stock" label="库存"  /> 
    </el-table>
    <div class="product-list-pagination">
    <el-pagination v-model:currentPage="pageInfo.current" :page-size="pageInfo.size" :total="pageInfo.total"
      layout="total, prev, pager, next" @current-change="handlePageChange"></el-pagination>
  </div>
  </el-row>

</template>

<style lang="scss" scoped>
.product-list-pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>